<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='文章管理',active='article'">
<header th:replace="admin/header::headerFragment(${title},${active})"></header>

<body class="fixed-left">
<div id="wrapper">
    <div th:replace="admin/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">文章管理</h4>
                    </div>
                    <div class="col-md-12">
                    	<select name="category" id="category">
                    		<option th:selected="${category == ''}" value="">全部</option>
                    		<option th:selected="${category == c.name}" th:each="c : ${categories}" th:value="${c.name}" th:text="${c.name}"></option>
                    	</select>
                    	&nbsp;&nbsp;&nbsp;&nbsp;
                    	<input type="button" id="tongbu" value="全量同步到es">
                    	&nbsp;&nbsp;&nbsp;&nbsp;
                    	<input type="button" id="refreshDefaultCover" value="刷新封面图片">
                    </div>
                    <br/>
                    <div class="col-md-12">
                        <table class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th width="20%">文章标题</th>
                                <th width="5%">置顶</th>
                                <th width="15%">简介</th>
                                <th width="15%">发布时间</th>
                                <th>浏览量</th>
                                <th>所属分类</th>
                                <th>标签</th>
                                <th width="8%">发布状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <th:block th:each="post : ${articles.list}">
                                <tr th:cid="${post.cid}">
                                    <td>
                                        <a th:href="@{'/admin/article/'+${post.cid}}" th:text="${post.title}"></a>
                                    </td>
                                    <td>
                                        <th:block th:if="${post.allowPing}">
                                            <span class="btn btn-danger">顶</span>
                                        </th:block>
                                        <th:block th:if="${!post.allowPing}">
                                            no
                                        </th:block>
                                    </td>
                                    <td>
                                        <p th:text="${(post.remark!=null and #strings.length(post.remark)>20)?(#strings.substring(post.remark,0,20)+'...'):post.remark}" th:title="${post.remark}"></p>
                                    </td>
                                    <td><th:block th:text="${commons.fmtdate(post.created, 'yyyy-MM-dd HH:mm:ss')}"/></td>
                                    <td><th:block th:text="${post.hits}"/></td>
                                    <td><th:block th:text="${post.categories}"/></td>
                                    <td><th:block th:text="${post.tags}"/></td>
                                    <td>
                                        <th:block th:if="${post.status == 'publish'}">
                                            <span class="label label-success">已发布</span>
                                        </th:block>
                                       <th:block th:if="${post.status == 'draft'}">
                                           <span class="label label-default">草稿</span>
                                       </th:block>
                                    </td>
                                    <td>
                                        <a th:href="@{'/admin/article/'+${post.cid}}"
                                           class="btn btn-primary btn-sm waves-effect waves-light m-b-5"><i
                                                class="fa fa-edit"></i> <span>编辑</span></a>
                                        <a href="javascript:void(0)" th:onclick="'delPost('+${post.cid}+');'"
                                           class="btn btn-danger btn-sm waves-effect waves-light m-b-5"><i
                                                class="fa fa-trash-o"></i> <span>删除</span></a>
                                        <a class="btn btn-warning btn-sm waves-effect waves-light m-b-5" th:href="@{${commons.permalink(post)}}"
                                           target="_blank"><i
                                                class="fa fa-rocket"></i> <span>预览</span></a>
                                    </td>
                                </tr>
                            </th:block>

                            </tbody>
                        </table>
                        <div th:replace="comm/macros :: pageAdminNav(${articles})"></div>
                    </div>
                </div>
                <div th:replace="admin/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>

<div th:replace="admin/footer :: footer"></div>
<script type="text/javascript">
    /*<![CDATA[*/
    var tale = new $.tale();
    function delPost(cid) {
        tale.alertConfirm({
            title:'确定删除该文章吗?',
            then: function () {
                tale.post({
                    url : ctxPath+'admin/article/delete',
                    data: {cid: cid},
                    success: function (result) {
                        if(result && result.success){
                            tale.alertOkAndReload('文章删除成功');
                        } else {
                            tale.alertError(result.msg || '文章删除失败');
                        }
                    }
                });
            }
        });
    }
    
    $("#category").on("change",function(){
    	window.location.href=ctxPath+"admin/article?page=1&category="+$("#category").val();
    });
    $(".movePage").on("click",function(){
    	console.log($(this).attr("href"));
    	var p = $(this).attr("href");
    	window.location.href=ctxPath+"admin/article"+p+"&category="+$("#category").val();
    	return false;
    });
	$("#tongbu").on("click", function(){
		var r=confirm("确认将文章全量同步到ES中吗？");
		  if (r==true){
				showLoading("<font color='black' size='8px' style='font-weight:bold'>wait...</font><img src='"+ctxPath+"admin/images/loading.gif'/>", 200, 50);

			  $.ajax({ 
				     type : "get", //提交方式 
				     url : ctxPath+"admin/article/tongbu",//路径
				     timeout : 60*60*1000, //超时时间设置，单位毫秒
				     success : function(result) {//返回数据根据结果进行相应的处理 
				    	 if(result.code==0){
				    		 tale.alertOkAndReload(result.msg);
				    	 }else{
				    		 tale.alertError(result.msg || '文章同步失败');
				    	 }
				    	 closeLoading();
				     },
				     error:function(arg1){
				    	 tale.alertError('文章同步失败');
				            closeLoading();
				        }
				    });
		    }
	});  
	
	$("#refreshDefaultCover").on("click", function(){
		var r=confirm("确认刷新默认文章封面图片吗？");
		  if (r==true){
				showLoading("<font color='black' size='8px' style='font-weight:bold'>同步中，请耐心等候，在完成前不要进行任何操作...</font><img src='"+ctxPath+"admin/images/loading.gif'/>", 200, 50);

			  $.ajax({ 
				     type : "get", //提交方式 
				     url : ctxPath+"admin/article/refreshDefaultCover",//路径
				     timeout : 60*60*1000, //超时时间设置，单位毫秒
				     success : function(result) {//返回数据根据结果进行相应的处理 
				    	 if(result.code==0){
				    		 tale.alertOkAndReload(result.msg);
				    	 }else{
				    		 tale.alertError(result.msg || '刷新失败');
				    	 }
				    	 closeLoading();
				     },
				     error:function(arg1){
				    	 tale.alertError('刷新失败');
				            closeLoading();
				        }
				    });
		    }
	}); 
    
	
	function showLoading(msg,msg_Width,msg_Height) {    

		var titleheight = "22px"; // 提示窗口标题高度    

		var bordercolor = "#666699"; // 提示窗口的边框颜色    

		var titlecolor = "#FFFFFF"; // 提示窗口的标题颜色    

		var titlebgcolor = "#666699"; // 提示窗口的标题背景色    

		var bgcolor = "#FFFFFF"; // 提示内容的背景色    

		var iWidth = document.documentElement.clientWidth;    

		var iHeight = document.documentElement.clientHeight;    

		var bgObj = document.createElement("div");

		    bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:" + iWidth + "px;height:" + Math.max(document.body.clientHeight, iHeight) + "px;filter:Alpha(Opacity=30);opacity:0.3;background-color:#000000;z-index:101;";    

		bgObj.id = "div_Top_Div1";    

		document.body.appendChild(bgObj);

		var msgObj = document.createElement("div");    

		msgObj.style.cssText = "position:absolute;top:" + (iHeight - msg_Height) / 2 + "px;left:" + (iWidth - msg_Width) / 2 + "px;width:" + msg_Width + "px;height:" + msg_Height + "px;text-align:center;padding:1px;line-height:22px;z-index:102;";     msgObj.id = "div_Top_Div2";    

		document.body.appendChild(msgObj);

		var table = document.createElement("table");    

		msgObj.appendChild(table);    

		table.style.cssText = "margin-top:20px;border:0px;padding:0px;";    

		table.cellSpacing = 0;;     var msgBox = table.insertRow(-1).insertCell(-1);    

		msgBox.style.cssText = "font:10pt '宋体';";    

		msgBox.colSpan = 2;     msgBox.innerHTML = msg; 
		
	}
	
	function closeLoading(){
		var removeObj1 = document.getElementById('div_Top_Div1');
		removeObj1.parentNode.removeChild(removeObj1);
		var removeObj2 = document.getElementById('div_Top_Div2');
		removeObj2.parentNode.removeChild(removeObj2);
	}
    /*]]>*/
</script>
</body>
</html>